#include "STLSMatrix.h"

Status InitMat(STLSMatrix *M)
{
    M->col = 0;
    M->row = 0;
    M->num = 0;
    M->L.item = (Triple *)malloc(SIZE * sizeof(Triple));
    M->L.size = SIZE;
    return OK;
}

Status GetMat(STLSMatrix *M)
{
    int c, index = 0, temp, sign;
    int mu, nu, tu;
    scanf("%d%d%d", &mu, &nu, &tu);
    getchar();

    M->row = mu;
    M->col = nu;
    M->num = tu;

    for (index = 0; index < tu; index++)
    {
        c = getchar();
        temp = 0;
        while (isdigit(c))
        {
            temp = temp * 10 + (c - '0');
            c = getchar();
        }
        temp *= sign;
        M->L.item[index].i = temp;
        temp = 0;
        while (!isdigit(c))
        {
            c = getchar();
        }

        while (isdigit(c))
        {
            temp = temp * 10 + (c - '0');
            c = getchar();
        }
        M->L.item[index].j = temp;
        temp = 0;

        while (!isdigit(c) && c != '-' && c != '+')
        {
            c = getchar();
        }
        if (c == '-')
        {
            sign = -1;
            c = getchar();
        }
        else if (c == '+')
        {
            sign = 1;
            c = getchar();
        }
        else 
        {
            sign = 1;
        }
        while (isdigit(c))
        {
            temp = temp * 10 + (c - '0');
            c = getchar();
        }
        temp *= sign;
        M->L.item[index].data = temp;
        temp = 0;
    }

    return OK;
}

Status TransMat(STLSMatrix *M, STLSMatrix *TM)
{
    int Num[SIZE], Cpot[SIZE];
    int i, col, q;

    for (i = 0; i < M->col; i++)
    {
        Num[i] = 0;
    }
    for (i = 0; i < M->num; i++)
    {
        Num[M->L.item[i].j]++;
    }
    Cpot[0] = 0;
    for (i = 1; i < M->col; i++)
    {
        Cpot[i] = Cpot[i - 1] + Num[i - 1];
    }

    TM->row = M->col;
    TM->col = M->row;
    TM->num = M->num;

    for (i = 0; i < M->num; i++)
    {
        col = M->L.item[i].j;
        q =  Cpot[col];
        TM->L.item[q].i = M->L.item[i].j;
        TM->L.item[q].j = M->L.item[i].i;
        TM->L.item[q].data = M->L.item[i].data;
        Cpot[col]++;
    }

    return OK;
}